# 测试工厂模式和单例模式的整合使用
class CarFactory:
    __obj = None  # 类属性
    __init_flag = True

    def create_car(self, brand):
        if brand == "奔驰":
            return Benz()
        elif brand == "宝马":
            return BMW()
        elif brand == "比亚迪":
            return BYD()
        else:
            return "未知品牌，无法创建"

    def __new__(cls, *args, **kwargs):
        if cls.__obj == None:
            cls.__obj = object.__new__(cls)

        return cls.__obj

    def __init__(self):
        if CarFactory.__init_flag:
            print("init CarFactory....")
            CarFactory.__init_flag = False


class Benz:
    pass


class BMW:
    pass


class BYD:
    pass


factory = CarFactory()
c1 = factory.create_car("奔驰")
c2 = factory.create_car("比亚迪")
print(c1)
print(c2)
# init CarFactory....
# <__main__.Benz object at 0x0000019169DB8050>
# <__main__.BYD object at 0x0000019169DB8830>

factory2 = CarFactory()
print(factory)
print(factory2)
# <__main__.CarFactory object at 0x0000019169A330E0>
# <__main__.CarFactory object at 0x0000019169A330E0>


# __new__是 创建类的对象时候进行创建
